﻿using Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFCouse
{
    /// <summary>
    /// ListBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ListBoxWindow : Window
    {
        public ListBoxWindow()
        {
            InitializeComponent();
          
        }
       

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 添加
            lbList.Items.Add("代洪");
            lbList.Items.Add(123);
            lbList.Items.Add(123);
            lbList.Items.Add(new ListBoxItem() { Content = "2022-01-20" });
            // 插入
            lbList.Items.Insert(3, "ssss");
            // 移出
            lbList.Items.Remove(123);
            // 移出项
            lbList.Items.Remove(item1);
            //项动态插入或添加，手动添加项，可移除
            //绑定而提供数据项，不能直接移除

            lbList.Items.Clear();//清空项
            List<ClassInfo> classList = GetClassList();
            lbList.ItemsSource = classList;//指定数据源
            lbList.DisplayMemberPath = "ClassName";
            lbList.SelectedValuePath = "ClassId";
            lbList.SelectedIndex = 0;//选择项
            lbList.SelectedValue = 7;//选择项
        }

        private DataTable GetClasses()
        {
            string sql = "select ClassId,ClassName from ClassInfos where GradeId=4";
            return SqlHelper.GetDataTable(sql, 1);
        }

        private List<ClassInfo> GetClassList()
        {
            DataTable dt = GetClasses();
            List<ClassInfo> list = new List<ClassInfo>();
            foreach (DataRow row in dt.Rows)
            {
                ClassInfo info = new ClassInfo()
                {
                    ClassId = int.Parse(row["ClassId"].ToString()),
                    ClassName = row["ClassName"].ToString()
                };
                list.Add(info);
              
            }
            return list;
        }
        List<string> strList = new List<string>();
        private void lbList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //手动添加的项  获取方式
            // lblNames.Content = (lbList.SelectedItem as ListBoxItem).Content.ToString();
            strList.Clear();
            var items = lbList.SelectedItems;
            //foreach (ListBoxItem item in items)
            //{
            //    strList.Add(item.Content.ToString());
            //}
            foreach (ClassInfo item in items)
            {
                strList.Add(item.ClassName);
            }
            lblNames.Content = string.Join(",", strList);
        }

        //提交获取选择项
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //var items = lbList.SelectedItems;
            //List<string> strList = new List<string>();
            //foreach(ListBoxItem item in items)
            //{
            //    strList.Add(item.Content.ToString());
            //}
            //lblNames.Content = string.Join(",", strList);
        }
    }
}
